<html>
	<head>
		<title>GradeGrid - View ClassRecord</title>
		<?php include("write_functions.php");?>
		<link rel="stylesheet" type="text/css" href="new.css" />
	</head>
	<body>
		<?php connect('localhost','root','','gradegriddatabase');?>
		<?php 
			$class_id = $_GET['classRec'];
			
			$index=0;
			$i=0;
			$in=0;
			$query_class="SELECT * FROM class";
			$result_class=mysql_query($query_class);
			$query_class_template = sprintf("SELECT template_id FROM class WHERE class_id='%s'",
			                                mysql_real_escape_string($class_id));

			$result_class_template = mysql_query($query_class_template);
			$class_template = mysql_result($result_class_template, $i, "template_id");
			
			$query_major="SELECT DISTINCT major.major_id, major.name, major.percentage FROM major, class WHERE major.template_id=class.template_id AND class.template_id='$class_template'";
			$result_major=mysql_query($query_major);
			$result_major=mysql_query($query_major);
			$numMajor=mysql_numrows($result_major);
			
			$query_students="SELECT account.last_name, account.first_name FROM account, take WHERE account.type='S' AND take.class_id='$class_id' AND take.account_id=account.account_id ORDER BY account.last_name, account.first_name";
			$result_students=mysql_query($query_students);
			$num_students=mysql_numrows($result_students);					//number of students in class
			
		?>
		<table border="1" class="pMirror">
			<tr>
				<td class="schedule">Student Names</td>
				<?php 
					while ($index<$numMajor):
						echo "<td>";
						$major_name=mysql_result($result_major,$index,"name");
						$major_percent=mysql_result($result_major,$index,"percentage");
						$major_id=mysql_result($result_major,$index,"major_id");
						$query_minor="SELECT minor.name, minor.maxrawscore FROM major,minor WHERE major.major_id='$major_id' AND major.major_id=minor.major_id ORDER BY minor.name";
						$result_minor=mysql_query($query_minor);
						$numMinor=mysql_numrows($result_minor);?>
						<?php echo "$major_name";?> ( <?php echo "$major_percent";?>%) 
						<?php if ($numMinor>0):
							$ix=0;
							$iz=0;?>	
							<table border="1" class="pMirror" align="center">
							<tr>
							<?php while ($ix<$numMinor):
									echo "<td align='center'>";
									$minor_name=mysql_result($result_minor,$ix,"name");
									$minor_maxraw=mysql_result($result_minor,$ix,"maxrawscore");
									echo "$minor_name";
									echo "</td>";
									$ix++;
								endwhile ?>
							</tr> 
							<tr>
							<?php while ($iz<$numMinor):
									echo "<td align='center'>";
									echo "$minor_maxraw";
									echo "</td>";//minor req
									$iz++;
								endwhile ?>
							</tr> 
							</table>
						<?php endif ?>
				<?php	echo "</td>";
						$index++;
					endwhile?>
			</tr>
			<?php while ($i<$num_students): 
				echo "<tr>";
					echo "<td>";
					$lastname = mysql_result($result_students, $i, "last_name");
					$firstname = mysql_result($result_students, $i, "first_name");
					echo "$lastname, $firstname";?>
			<?php 	echo "</td>";
					$query_grade_num="SELECT grade FROM grade,account WHERE account.username='$class_list' AND grade.account_id=account.account_id AND grade.class_id='$class_id'";
					$result_numGrade=mysql_query($query_grade_num);
					$num_grade=mysql_numrows($result_numGrade);
					
					while ($in<$num_grade){
						$major_id1=mysql_result($result_major,$in,"major_id");
						$query_grade="SELECT DISTINCT grade FROM account,take,grade,major WHERE grade.class_id='$class_id' AND take.class_id='$class_id' AND major.major_id='$major_id1' AND grade.account_id = account.account_id AND account.username='$class_list' AND grade.class_id='$class_id'";
						$result_grade=mysql_query($query_grade);
						echo "<td>";
						$grade=mysql_result($result_grade,$in,"grade");
						echo "$grade";
						echo "</td>";
						$in++;
					}
				$i++;
				echo "</tr>";
			endwhile?>
				
			</tr>
		</table>
		<?php mysql_close();?>
	</body>
</html>